import request from '../request.js';

const baseUrl = 'http://localhost:3000';

function getTodoList() {
    return request({
        url: baseUrl + '/todos'
    })
        .then(data => {
            return data;
        })
        .catch(err => {
            console.error(err.message);
            throw err;
        });
}

function addTodo(title, completed = "1") {
    return request({
        url: baseUrl + '/todos',
        method: 'POST',
        data: { title: title, completed: completed }
    })
        .then(data => {
            return data;
        })
        .catch(err => {
            console.error(err.message);
            throw err;
        });
}

function delTodo(id) {
    return request({
        url: baseUrl + `/todos/${id}`,
        method: 'DELETE'
    })
        .then(data => {
            return data;
        })
        .catch(err => {
            console.error(err.message);
            throw err;
        });
}

export { getTodoList, addTodo, delTodo };